package com.itly.controller;

import com.itly.entity.Replay;
import com.itly.service.ReplayService;
import com.itly.util.ApiResult;
import com.itly.util.ApiResultHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ReplayController {
    private Logger logger = LoggerFactory.getLogger(ReplayController.class);

    @Autowired
    private ReplayService replayService;

    @PostMapping("replay")
    public ApiResult replay(@RequestBody Replay replay) {
        int i = replayService.addReplay(replay);
        if (i == 1) {
            logger.info("回复留言成功");
            return ApiResultHandler.buildApiResult(200, "回复留言成功", i);
        } else {
            logger.info("回复留言失败",replay);
            return ApiResultHandler.buildApiResult(400, "回复留言失败", i);
        }
    }

    @GetMapping("replay/{messageId}")
    public ApiResult selectById(@PathVariable("messageId") Integer messageId) {
        List<Replay> replays = replayService.selectById(messageId);
        return ApiResultHandler.buildApiResult(200, "查找留言", replays);
    }
}
